package com.application.fabric.users.controller;


import com.application.fabric.fabriccontrol.config.PDFmsg;
import com.application.fabric.fabriccontrol.existevidence.CreatePDF;
import com.application.fabric.fabriccontrol.existevidence.PDFService;
import com.application.fabric.users.config.Dealmsg;

import com.application.fabric.users.config.HDFS.HadoopTemplate;
import com.application.fabric.users.entity.Evidence;
import com.application.fabric.users.entity.ExtractToken;
import com.application.fabric.users.entity.Returnmsg;
import com.application.fabric.users.service.Exsiting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.Map;

@RestController
@RequestMapping("/evidence")
public class Upload {
    @Autowired
    private Dealmsg dealmsg;
    @Resource
    private Returnmsg returnmsg;

    @Autowired
    private HadoopTemplate hadoopTemplate;
//    创建用户目录
    @Resource
    public ExtractToken token;
//    存证



    @Autowired
    private PDFService pdfService;
    //接收前端文件流
//    引入存证服务层
    @Autowired
    private Exsiting exsiting;
//        public Map<String,Object> getfileload(@RequestParam("file") MultipartFile file) throws Exception
   @PostMapping( "getfileload")
    public Map<String,Object> getfileload(Evidence evidence) throws Exception {
//   设置存证静态名字
       evidence.setExistname(evidence.getExistname());
       evidence.setUserid("appUser");
       String hdfspath = "/existingevidence/"+evidence.getFile().getOriginalFilename();
//       hdfs用户目录创建
       String hdfsuserpath = "/existingevidence"+token.getUserid();
       Boolean flag = hadoopTemplate.existDir("/existingevidence",false);
       if (flag){
           if (!evidence.getFile().isEmpty()){
               exsiting.intoexsit(evidence);
//               证书上传
               pdfService.uploadce(evidence);
           }
       }

//
       returnmsg.setData("成功");
       returnmsg.setCode(200);
       return  dealmsg.successmsg(returnmsg);

   }


    //   出证查询 outevidence  暂时使用useid ceshi
   @GetMapping("outevidenceall")
   public Map<String,Object> outevidenceall(@RequestParam("userid") String userid) throws Exception {
//       System.out.println(userid);
//       exsiting.outevidenceallservice();
       returnmsg.setData(exsiting.outevidenceallservice(userid));
       returnmsg.setCode(200);
       return  dealmsg.successmsg(returnmsg);
   }




}
